<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/10
 * Time: 16:55
 */

namespace Admin\Controller;
use Think\Controller;

class FinanceController extends Controller{
    public function index(){

    }
    //已付定金
    public function firstPayList(){
        $obj = M("order_money");
        $obj2 = M("order_main");
        $obj3 = M("user_main");
        $arrs = $obj->where("pay_status=1")->select();
        $datas =array();
        $payArr = array();
        foreach($arrs as $arr){
            $omid = $arr["omid"];
            $mainArr = $obj2->where("omid=".$omid)->find();
            $payArr["order_num"] = $mainArr["ordernum"];
            $payArr["num"] = $mainArr["count"];
            $payArr["price_count"] = $mainArr["price_count"];
            $payArr["dj_count"] = $mainArr["dj_count"];
            $payArr["status"] = $mainArr["status"];
            $payArr["belong_gc"]=$mainArr["belong_gc"];
            $uid = $mainArr["uid"];
            $userArr = $obj3->where("uid=".$uid)->find();
            $payArr["name"] = $userArr["name"];
            $payArr["phone"] = $userArr["phone"];
            $datas[] = $payArr;
        }
        $this->assign("datas",$datas);
        $this->display();
    }
    //选择已付定金的方式
    public function firstPayType($id,$type){
        $obj = M("order_money");
        $data["first_pay_type"] = $type;
        $rs = $obj->where("omid=".$id)->save($data);
        if($rs == false){
            $this->error('失败','../../../../firstPayList');
        }else{

        }
    }
    //工厂成本价格列表
    public function gcPayList(){
        $obj = M("order_profile");
        $obj2 = M("user_main");
        //查询所有订单
        $count = $obj->order('order_time desc')->count();
        $page = getPage($count);
        $profileArr = $obj->order('order_time desc')->limit($page->firstRow.','.$page->listRows)->select();
        $datas=[];
        $arr2=[];
        foreach($profileArr as $profile){
            $arr2["order_time"] = $profile["order_time"];
            $arr2["order_num2"] = $profile["ordernum2"];
            if($profile["pro_id"] ==2){
                $arr2["color"]=$profile["xf_color"];
                $xf_color = substr($profile["xf_color"],0,1);
                $arr2["num"]=$profile["xf_num"];
             }else{
                $arr2["color"]=$profile["color"];
                $csh_color = substr($profile["color"],0,1);
                $arr2["num"]=$profile["num"];
            }
            $arr2["type"]=$profile["xf_type"];
            //面料和类型得出相应的成本价
            //衬衫
            if($profile["xf_type"] == '长袖衬衫' || $profile["xf_type"]=='短袖衬衫'){
                if($csh_color=='8'){
                    $arr2["gc_price"] = 90;
                }elseif($csh_color == 'H' || $csh_color=='7'){
                    $arr2["gc_price"] = 140;
                }elseif($csh_color == 'A'){
                    $arr2["gc_price"] = 250;
                }else{
                    $arr2["gc_price"] = '暂无数据';
                }
            }
            //套西
            if($profile["xf_type"] == '套西'){
                if($xf_color == 'E'){
                    $arr2["gc_price"] = 450;
                }elseif($xf_color == 'Y'){
                    $arr2["gc_price"] = 750;
                }elseif($xf_color == 'H'){
                    $arr2["gc_price"] = 1500;
                }else{
                    $arr2["gc_price"] = '暂无数据';
                }
            }
            //单西
            if($profile["xf_type"] == '单西'){
                if($xf_color == 'E'){
                    $arr2["gc_price"] = 300;
                }elseif($xf_color == 'Y'){
                    $arr2["gc_price"] = 500;
                }elseif($xf_color == 'H'){
                    $arr2["gc_price"] = 1000;
                }else{
                    $arr2["gc_price"] = '暂无数据';
                }
            }
            //单裤
            if($profile["xf_type"] == '西裤' || $profile["xf_type"] == '裙子'){
                if($xf_color == 'E'){
                    $arr2["gc_price"] = 150;
                }elseif($xf_color == 'Y'){
                    $arr2["gc_price"] = 250;
                }elseif($xf_color == 'H'){
                    $arr2["gc_price"] = 500;
                }else{
                    $arr2["gc_price"] = '暂无数据';
                }
            }

            $datas[]=$arr2;
        }
        $this->assign("datas",$datas);
        $this->assign("page", $page->show());
        $this->display();
    }
    //工厂列表导出
    public function gcListExport(){
        $postData = I("post.");
        $obj = M("order_profile");
        $obj2 = M("user_main");
        $list = $obj->where("order_time>=UNIX_TIMESTAMP('" . $postData["start_time"] . "') and order_time<UNIX_TIMESTAMP('" . $postData["end_time"] . "')")->order("order_time desc")->select();
        $datas = [];
        foreach ($list as $l) {
            $data["order_time"] = date("Y-m-d",$l["order_time"]);
            //订单信息
            $data["orderNum2"] = $l["ordernum2"];
            $data["xf_type"] = $l["xf_type"];

            //衬衣和西服
            if($l["pro_id"] ==2){
                $data["num"] = $l["xf_num"];
                $data["color"] = $l["xf_color"];
                $xf_color = substr($l["xf_color"],0,1);
            }else{
                $data["num"] = $l["num"];
                $data["color"] = $l["color"];
                $csh_color=substr($l["color"],0,1);
            }
            //衬衫
            if($l["xf_type"] == '长袖衬衫' || $l["xf_type"]=='短袖衬衫'){
                if($csh_color=='8'){
                    $data["gc_price"] = 90;
                }elseif($csh_color == 'H' || $csh_color=='7'){
                    $data["gc_price"] = 140;
                }elseif($csh_color == 'A'){
                    $data["gc_price"] = 250;
                }else{
                    $data["gc_price"] = '暂无数据';
                }
            }
            //套西
            if($l["xf_type"] == '套西'){
                if($xf_color == 'E'){
                    $data["gc_price"] = 450;
                }elseif($xf_color == 'Y'){
                    $data["gc_price"] = 750;
                }elseif($xf_color == 'H'){
                    $data["gc_price"] = 1500;
                }else{
                    $data["gc_price"] = '暂无数据';
                }
            }
            //单西
            if($l["xf_type"] == '单西'){
                if($xf_color == 'E'){
                    $data["gc_price"] = 300;
                }elseif($xf_color == 'Y'){
                    $data["gc_price"] = 500;
                }elseif($xf_color == 'H'){
                    $data["gc_price"] = 1000;
                }else{
                    $data["gc_price"] = '暂无数据';
                }
            }
            //单裤
            if($l["xf_type"] == '西裤' || $l["xf_type"] == '裙子'){
                if($xf_color == 'E'){
                    $data["gc_price"] = 150;
                }elseif($xf_color == 'Y'){
                    $data["gc_price"] = 250;
                }elseif($xf_color == 'H'){
                    $data["gc_price"] = 500;
                }else{
                    $data["gc_price"] = '暂无数据';
                }
            }
            //裙子
            if($l["xf_type"] == '裙子'){
                if($xf_color == 'E'){
                    $data["gc_price"] = 150;
                }elseif($xf_color == 'Y'){
                    $data["gc_price"] = 250;
                }elseif($xf_color == 'H'){
                    $data["gc_price"] = 500;
                }else{
                    $data["gc_price"] = '暂无数据';
                }
            }
            $datas[] = $data;
        }
        exportexcel($datas, ["日期", "编号", "衣服类型", "数量", "面料", "成本"], "gcOrderList");
    }
    //量体提成列表
    public function ltPayList(){
        $obj = M("order_profile");
        //查询所有订单
        $count = $obj->order('order_time desc')->count();
        $page = getPage($count);
        $profileArr = $obj->order('order_time desc')->limit($page->firstRow.','.$page->listRows)->select();
        $datas=[];
        $arr2=[];
        foreach($profileArr as $profile){
            $arr2["order_time"] = $profile["order_time"];
            $arr2["order_num2"] = $profile["ordernum2"];
            $arr2["belong"] = $profile["belong"];
            if($profile["pro_id"] ==2){
                $arr2["color"]=$profile["xf_color"];
                $xf_color = substr($profile["xf_color"],0,1);
                $arr2["num"]=$profile["xf_num"];
            }else{
                $arr2["color"]=$profile["color"];
                $csh_color = substr($profile["color"],0,1);
                $arr2["num"]=$profile["num"];
            }
            $arr2["type"]=$profile["xf_type"];
            //面料和类型得出相应的成本价
            //衬衫
            if($profile["xf_type"] == '长袖衬衫' || $profile["xf_type"]=='短袖衬衫'){
                    $arr2["lt_price"] = 5;
            }
            //套西
            if($profile["xf_type"] == '套西'){
                $arr2["lt_price"] = 15;
            }
            //单西
            if($profile["xf_type"] == '单西'){
                $arr2["lt_price"] = 10;
            }
            //单裤、裙子
            if($profile["xf_type"] == '西裤' || $profile["xf_type"] == '裙子'){
                $arr2["lt_price"] = 5;
            }

            $datas[]=$arr2;
        }
        $descr = "lt";
        $this->assign("descr",$descr);
        $this->assign("datas",$datas);
        $this->assign("page", $page->show());
        $this->display("Finance/gcPayList");
    }
    //量体提成导出excel
    public function ltPayExport(){
        $postData = I("post.");
        $obj = M("order_profile");
        $list = $obj->where("order_time>=UNIX_TIMESTAMP('" . $postData["start_time"] . "') and order_time<UNIX_TIMESTAMP('" . $postData["end_time"] . "')")->order("order_time desc")->select();
        $datas = [];
        foreach ($list as $l) {
            $data["order_time"] = date("Y-m-d",$l["order_time"]);
            //订单信息
            $data["orderNum2"] = $l["ordernum2"];
            $data["belong"] = $l["belong"];
            $data["xf_type"] = $l["xf_type"];

            //衬衣和西服
            if($l["pro_id"] ==2){
                $data["num"] = $l["xf_num"];
                $data["color"] = $l["xf_color"];
                $xf_color = substr($l["xf_color"],0,1);
            }else{
                $data["num"] = $l["num"];
                $data["color"] = $l["color"];
                $csh_color=substr($l["color"],0,1);
            }
            //衬衫
            if($l["xf_type"] == '长袖衬衫' || $l["xf_type"]=='短袖衬衫'){
                    $data["lt_price"] = 5;
            }
            //套西
            if($l["xf_type"] == '套西'){
                $data["lt_price"] = 15;
            }
            //单西
            if($l["xf_type"] == '单西'){
                $data["lt_price"] = 10;
            }
            //单裤
            if($l["xf_type"] == '西裤' || $l["xf_type"] == '裙子'){
                $data["lt_price"] = 5;
            }

            $datas[] = $data;
        }
        exportexcel($datas, ["日期", "编号", "衣服类型", "数量", "面料", "提成"], "ltOrderList");
    }

    //收款
    public function collectionList(){
        $obj = M("order_main");
        $user = M("user_main");
        $orderp = M("order_profile");
        $money = M("order_money");
        $count = $obj->order('gc_updated desc')->count();
        $page = getPage($count);
        $mainArr = $obj->order('gc_updated desc')->
        limit($page->firstRow.','.$page->listRows)->select();
        $datas=[];
        $arr2=[];
        foreach($mainArr as $main){
            $arr2["omid"] = $main["omid"];
            $arr2["gc_updated"] = $main["gc_updated"];
            //查询名字
            $userArr = $user->where("uid =".$main["uid"])->find();

            $arr2["name"] = $userArr["name"];
            $arr2["ordernum"] = $main["ordernum"];

            //查询衣服类型
            $orderpArr = $orderp->where("omid=".$main["omid"])->select();
            $type = [];
            foreach($orderpArr as $p){
                $type[] = $p["xf_type"];
            }
            $arr2["type"] = implode("+",$type);

            $arr2["num"] = $main["count"];
            //查询order_money表
            $moneyArr = $money->where("omid=".$main["omid"])->find();
            $arr2["price_count"] = $main["price_count"];//总金额
            $arr2["dj_count"] = $main["dj_count"];//定金
            $arr2["dj_ren"] = $moneyArr["dj_ren"];
            $arr2["dj_updated"] = $moneyArr["dj_updated"];
            $arr2["dj_type"] = $moneyArr["dj_type"];
            $arr2["balance"] = $main["price_count"]-$main["dj_count"]; //余额
            $arr2["ba_ren"] = $moneyArr["ba_ren"]; //余额收款人
            $arr2["ba_updated"] = $moneyArr["ba_updated"]; //余额日期
            $arr2["ba_type"] = $moneyArr["ba_type"]; //余额方式

            $datas[]=$arr2;
        }
        $this->assign("datas",$datas);
        $this->assign("page", $page->show());
        $this->display();
    }

    //定金支付方式
    public function djPay(){
        $data = I("post.");
        $money = M("order_money");
        $arr = $money->where("omid=".$data["omid"])->find();
        if(empty($arr)){
            $rs = $money->add($data);
            if($rs == false){
                $this->error("提交失败","../collectionList");
            }else{
                redirect("collectionList");
            }
        }else{
            $rs = $money->where("omid=".$data["omid"])->save($data);
            if($rs == false){
                $this->error("提交失败","../collectionList");
            }else{
                redirect("collectionList");
            }
        }
    }

    //余额支付方式
    public function balancePay(){
        $data = I("post.");
        $money = M("order_money");
        $arr = $money->where("omid=".$data["omid"])->find();
        if(empty($arr)){
            $rs = $money->add($data);
            if($rs == false){
                $this->error("提交失败","../collectionList");
            }else{
                redirect("collectionList");
            }
        }else{
            $rs = $money->where("omid=".$data["omid"])->save($data);
            if($rs == false){
                $this->error("提交失败","../collectionList");
            }else{
                redirect("collectionList");
            }
        }
    }

    //修改金额
    public function moneyEdit(){
        $data = I("post.");
        $orderm = M("order_main");
        $rs = $orderm->where("omid=".$data["omid"])->save($data);
        if($rs == false){
            $this->error("提交失败","collectionList");
        }else{
            redirect("collectionList");
        }
    }

    //收款列表导出excel
    public function collectionListExport(){
        $postData = I("post.");
        $obj = M("order_main");
        $user = M("user_main");
        $orderp = M("order_profile");
        $money = M("order_money");
        $list = $obj->where("gc_updated>=UNIX_TIMESTAMP('" . $postData["start_time"] . "') and gc_updated<UNIX_TIMESTAMP('" . $postData["end_time"] . "')")->order("order_time desc")->select();
        $datas = [];
        foreach ($list as $l) {
            $arr2["gc_updated"] = date("Y-m-d",$l["gc_updated"]);
            //姓名
            $userArr = $user->where("uid =".$l["uid"])->find();

            $arr2["name"] = $userArr["name"];
            $arr2["ordernum"] = $l["ordernum"];

            //查询衣服类型
            $orderpArr = $orderp->where("omid=".$l["omid"])->select();
            $type = [];
            foreach($orderpArr as $p){
                $type[] = $p["xf_type"];
            }
            $arr2["type"] = implode("+",$type);

            $arr2["num"] = $l["count"];
            //查询order_money表
            $moneyArr = $money->where("omid=".$l["omid"])->find();
            $arr2["price_count"] = $l["price_count"];//总金额
            $arr2["dj_count"] = $l["dj_count"];//定金
            $arr2["dj_ren"] = $moneyArr["dj_ren"];
            $arr2["dj_updated"] = $moneyArr["dj_updated"];
            $arr2["dj_type"] = $moneyArr["dj_type"];
            $arr2["balance"] = $l["price_count"]-$l["dj_count"]; //余额
            $arr2["ba_ren"] = $moneyArr["ba_ren"]; //余额收款人
            $arr2["ba_updated"] = $moneyArr["ba_updated"]; //余额日期
            $arr2["ba_type"] = $moneyArr["ba_type"]; //余额方式

            $datas[]=$arr2;
        }
        exportexcel($datas, ["分配日期", "姓名", "订单号", "明细", "件数", "总金额",
            "订金","订金收款人","订金收款日期","订金收款方式","余款","余款收款人","余款收款日期","余款收款方式"],
            "收支表");
    }
} 